package com.usefullc.system.service.component.risk;

import com.usefullc.system.service.AbstractService;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

//import com.usefullc.system.service.RoleAuthResService;

/**
 * @author: Sampson
 * @date: 2022-01-10 13:44
 */
@Component
public class RequestResComponent extends AbstractService {

//    @Value("${auth.check}")
//    private boolean authCheck;
//
//
//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private AuthResService authResService;
//
//    @Autowired
//    private RoleService roleService;
//
////    @Autowired
////    private RoleAuthResService roleAuthResService;
//
//    @Autowired
//    private SystemConfigComponent systemConfigComponent;
//
//    private Map<Long, Set<AuthResObj>> roleAuthResMap = new HashMap<>();
    private Map<String, RequestResource> riskUrlAuthResMap = new HashMap<>();
    private Map<String, RequestResource> repeatUrlAuthResMap = new HashMap<>();
//    private Set<String> whiteUrls = new HashSet<>();
//
//    /**
//     * 初始化权限资源
//     */
//    public void initAuthRes(){
//        if(authInit) {
//            List<AuthRes> authResFromContext = authResService.findAuthResFromContext(systemConfigComponent.getAppName());
////            authResService.saveAuthRees(authResFromContext); /TODO
//        }
//    }
//    @EventListener(ApplicationReadyEvent.class)
//    public void initData(){
//        initAuthRes();
//        roleAuthResMap.clear();
//        //获取所有可用资源
//        AuthResQuery authResQuery = new AuthResQuery();
//        String appName = systemConfigComponent.getAppName();
//        authResQuery.setAppType(appName);
//        authResQuery.setStatus(YesNoEnum.YES.getCode());
//        List<AuthRes> authResList = authResService.findAll(authResQuery);
//
//        if (CollectionUtils.isEmpty(authResList)) {
//            return;
//        }
//        Map<Long, AuthResObj> authResMap = authResList.stream().filter(x -> x.getAuthCheck()).collect(Collectors.toMap(AuthRes::getId, authRes -> {
//            AuthResObj authResObj = new AuthResObj();
//            authResObj.setCode(authRes.getCode());
//            authResObj.setUrl(authRes.getUrl());
//            return authResObj;
//        }));
//
//
//        riskUrlAuthResMap = authResList.stream().filter(x -> x.getRiskCheck()).collect(Collectors.toMap(AuthRes::getUrl, authRes -> {
//            AuthResObj authResObj = new AuthResObj();
//            authResObj.setCode(authRes.getCode());
//            authResObj.setUrl(authRes.getUrl());
//            authResObj.setRiskCheck(authRes.getRiskCheck());
//            authResObj.setRiskCheckCondition(authRes.getRiskCheckCondition());
//            return authResObj;
//        }));
//
//        repeatUrlAuthResMap = authResList.stream().filter(x -> x.getRepeatCheck()).collect(Collectors.toMap(AuthRes::getUrl, authRes -> {
//            AuthResObj authResObj = new AuthResObj();
//            authResObj.setCode(authRes.getCode());
//            authResObj.setUrl(authRes.getUrl());
//            authResObj.setRiskCheck(authRes.getRepeatCheck());
//            return authResObj;
//        }));
//
//        whiteUrls = authResList.stream().filter(x -> !x.getAuthCheck()).map(x -> x.getUrl()).collect(Collectors.toSet());
//
//        //手动加入
//        whiteUrls.add("/tools/initData");
//        whiteUrls.add("/task/test");
//        whiteUrls.add("swagger-ui.html");
//        whiteUrls.add("/liveStatus");
//        whiteUrls.add("/readyStatus");
//        whiteUrls.add("/redirectHttpStatus");
//
//
//        //建立角色资源对应关系
////        RoleAuthResQuery roleAuthResQuery = new RoleAuthResQuery();
//
//        List<Role> roleList = roleService.findAll(new RoleQuery());
//        List<AuthRes> dataList = new ArrayList<>();
//        for (Role role : roleList) {
//            List<AuthRes> resList = role.getAuthResList();
//            for (AuthRes authRes : resList) {
//                if (!dataList.contains(authRes)) {
//                    MapUtils.putSetElement(roleAuthResMap,role.getId(),authResMap.get(authRes.getId()));
//                }
//            }
//        }
//
////        //排序
////        Collections.sort(dataList, Comparator.comparingInt(AuthRes::getOrderNo));
////        for (AuthRes authRes : dataList) {
////            MapUtils.putSetElement(roleAuthResMap,roleId,authResMap.get(authResId));
////        }
////        List<RoleAuthRes> roleAuthResList = roleAuthResService.findAll(roleAuthResQuery);
////        if (CollectionUtils.isEmpty(roleAuthResList)) {
////            return;
////        }
////        for (RoleAuthRes roleAuthRes : roleAuthResList) {
////            Long roleId = roleAuthRes.getRoleId();
////            Long authResId = roleAuthRes.getAuthResId();
////            AuthResObj authResObj = authResMap.get(authResId);
////            if(authResObj == null){
////                continue;
////            }
////
////        }
//    }
//
//    public boolean checkAuth(HttpServletRequest request){
//        //现在都在数据中去标记
//        UserVo loginUser = userService.getLoginUser(request);
//
//        //将当前访问用户放入threadLocal
//        UserManager.setLoginUser(loginUser);
//        OnlineUser onlineUser = new OnlineUser(loginUser.getId(), loginUser.getUserName());
//        OnlineUserManager.setLoginUser(onlineUser);
//
//        //不要检查
//        if(!authCheck){
//            return true;
//        }
//
//        String requestURI = request.getRequestURI();
//        if(whiteUrls.contains(requestURI)){
//            return true;
//        }
//
//        if(loginUser == null){
//            throw new BizException( SystemConstant.ResponseCode.NOT_LOGIN,"请登录");
//        }
//
//        //暂时默认get请求都通过,后续通过资源做标记，获取所有资源去匹配
////        if (request.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())) {
////            return true;
////        }
//
//        List<Long> roleIds = loginUser.getRoleIds();
//        if (CollectionUtils.isEmpty(roleIds)) {
//            throw new BizException("user role is null");
//        }
//
//        for (Long roleId : roleIds) {
//            Set<AuthResObj> authResSet = roleAuthResMap.get(roleId);
//            if(CollectionUtils.isEmpty(authResSet)){
//                continue;
//            }
//            boolean isMatch = authResSet.stream().anyMatch(x -> StringUtils.equals(x.getUrl(),requestURI));
//            if(isMatch){
//                return true;
//            }
//        }
//        return false;
//
//    }


    public Map<String, RequestResource> getRiskUrlAuthResMap() {
        return riskUrlAuthResMap;
    }

    public Map<String, RequestResource> getRepeatUrlAuthResMap() {
        return repeatUrlAuthResMap;
    }



//    public  Set<AuthResObj> findAuthResByRoleIds(List<Long> roleIds){
//        Set<AuthResObj> authResObjSet = new HashSet<>();
//        for (Long roleId : roleIds) {
//            Set<AuthResObj> authResObjs = roleAuthResMap.get(roleId);
//            authResObjSet.addAll(authResObjs);
//        }
//        return authResObjSet;
//    }
}
